﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement.DocumentSets;

namespace MS.InformationManagement.Core.Actions.Informado
{
    public class UpdateSourceDocumentCommand : IBaseCommand
    {
        #region [Private Fields]

        private SPFolder _sourceFolder;
        private string _selectedUser;
        private SPWeb _web;
        private DocumentSet _currentDocument;
        private DateTime _logDate; 
        
        #endregion

        #region [Constructors]
        
        public UpdateSourceDocumentCommand(SPFolder sourceFolder, string selectedUser, SPWeb web)
        {
            _sourceFolder = sourceFolder;
            _selectedUser = selectedUser;
            _web = web;
        }
 
        #endregion

        #region [IBaseCommand Members]

        public void Execute()
        {
            _logDate = DateTime.Now;
            _currentDocument = Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.GetDocumentSet(_sourceFolder);
            _currentDocument.Log(string.Empty, _logDate, InformationManagement.Support.Enums.TraceableOperation.Informado, _web.CurrentUser.Name
                , string.Format("Documento Informado al usuario {0}", _selectedUser));
            _currentDocument.Folder.Item.Update();
        }

        public void Undo()
        {
            _currentDocument.RemoveLog(_logDate);
            _currentDocument.Folder.Item.Update();
        }
 
        #endregion
    }
}
